package test.threads.queue.stressed.cpu;

import java.lang.reflect.Field;

import com.google.common.base.Throwables;
import com.lmax.disruptor.EventFactory;

/**
* @author cheremin
* @since 29.02.12,  14:10
*/
public final class LongValueMessage {
//        public volatile long $a01, $a02, $a03, $a04, $a05, $a06, $a07, $a08;
    public long value = 0;
//        public volatile long $b01, $b02, $b03, $b04, $b05, $b06, $b07, $b08;

    public LongValueMessage() {
        preventPaddingElimination();
    }

    public LongValueMessage( final long value ) {
        this();
        this.value = value;
    }

    public long preventPaddingElimination() {
        try {
            final Field[] fields = this.getClass().getFields();
            long sum = 0;
            for ( final Field field : fields ) {
                if ( field.getName().startsWith( "$" ) ) {
                    sum += ( ( Number ) field.get( this ) ).longValue();
                }
            }
            return sum;
        } catch ( IllegalAccessException e ) {
            throw Throwables.propagate( e );
        }
    }

    public static final EventFactory<LongValueMessage> FACTORY = new EventFactory<LongValueMessage>() {
        @Override
        public LongValueMessage newInstance() {
            return new LongValueMessage();
        }

        @Override
        public String toString() {
            return "Message.new()";
        }
    };
}
